package com.wyzz.hr.exportfile;

import java.io.IOException;
import java.util.List;

import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.TiaoxiuBean;

import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

public class ExportTiaoxiu extends SingleSheetExportHandler {

	public ExportTiaoxiu(List result, String sheetTitle) {
		super(result, sheetTitle);
	}

	protected void buildBody() throws RowsExceededException, WriteException,
			IOException {
		int size = result.size();
		for (int i = 0; i < size; i++) {
			TiaoxiuBean bean = (TiaoxiuBean) result.get(i);
			Employee emp = bean.getEmp();
			String data[][] = bean.getResult();
			writableSheet.setRowView(i + 3, 500);
			writableSheet.addCell(new Label(0, i + 3, emp.getEmpDistinctNo(),
					defaultCellFormat));
			writableSheet.addCell(new Label(1, i + 3, emp.getEmpName(),
					defaultCellFormat));
			if (emp.getEmpDeptNo() != null)
				writableSheet.addCell(new Label(2, i + 3, emp.getEmpDeptNo()
						.getDepartmentName(), defaultCellFormat));
			if (emp.getEmpDeptNo1() != null)
				writableSheet.addCell(new Label(3, i + 3, emp.getEmpDeptNo1()
						.getDepartmentName(), defaultCellFormat));
			if (emp.getEmpDeptNo2() != null)
				writableSheet.addCell(new Label(4, i + 3, emp.getEmpDeptNo2()
						.getDepartmentName(), defaultCellFormat));
			if (emp.getEmpDeptNo3() != null)
				writableSheet.addCell(new Label(5, i + 3, emp.getEmpDeptNo3()
						.getDepartmentName(), defaultCellFormat));
			if (emp.getEmpPbNo() != null)
				writableSheet.addCell(new Label(6, i + 3, emp.getEmpPbNo()
						.getPbName(), defaultCellFormat));
			if (data == null)
				continue;
			for (int j = 0; j < data.length; j++) {
				String month[] = data[j];
				String commonArray[] = month[1].split("-");
				String weekEndArray[] = month[2].split("-");
				String holidayArray[] = month[3].split("-");
				writableSheet.addCell(new Number(j * 6 + 7, i + 3, Float
						.parseFloat(commonArray[0]), numberCellFormat));
				writableSheet.addCell(new Number(j * 6 + 8, i + 3, Float
						.parseFloat(commonArray[1]), numberCellFormat));
				writableSheet.addCell(new Number(j * 6 + 9, i + 3, Float
						.parseFloat(weekEndArray[0]), numberCellFormat));
				writableSheet.addCell(new Number(j * 6 + 10, i + 3, Float
						.parseFloat(weekEndArray[1]), numberCellFormat));
				writableSheet.addCell(new Number(j * 6 + 11, i + 3, Float
						.parseFloat(holidayArray[0]), numberCellFormat));
				writableSheet.addCell(new Number(j * 6 + 12, i + 3, Float
						.parseFloat(holidayArray[1]), numberCellFormat));
			}

		}

		writableWorkbook.write();
	}

	protected void buildTitle() throws RowsExceededException, WriteException {
		writableSheet = writableWorkbook.createSheet(sheetTitle, 0);
		String data[][] = (String[][]) null;
		int i = 0;
		do {
			if (i >= result.size())
				break;
			TiaoxiuBean tx = (TiaoxiuBean) result.get(i);
			if (tx.getResult() != null) {
				data = tx.getResult();
				break;
			}
			i++;
		} while (true);
		int num = data.length;
		writableSheet.setRowView(0, 500);
		writableSheet.setColumnView(0, 15);
		writableSheet.setColumnView(1, 15);
		writableSheet.setColumnView(2, 15);
		writableSheet.setColumnView(3, 15);
		writableSheet.setColumnView(4, 15);
		writableSheet.setColumnView(5, 15);
		writableSheet.setColumnView(6, 15);
		writableSheet.addCell(new Label(0, 0, "\u5DE5\u53F7", titleCellFormat));
		writableSheet.addCell(new Label(1, 0, "\u59D3\u540D", titleCellFormat));
		writableSheet.addCell(new Label(2, 0, "\u90E8\u95E8", titleCellFormat));
		writableSheet.addCell(new Label(3, 0, "\u4E00\u7EA7\u90E8\u95E8",
				titleCellFormat));
		writableSheet.addCell(new Label(4, 0, "\u4E8C\u7EA7\u90E8\u95E8",
				titleCellFormat));
		writableSheet.addCell(new Label(5, 0, "\u4E09\u7EA7\u90E8\u95E8",
				titleCellFormat));
		writableSheet.addCell(new Label(6, 0, "\u804C\u4F4D", titleCellFormat));
		for (i = 0; i < num; i++) {
			writableSheet.addCell(new Label(i * 6 + 7, 0, data[i][0],
					titleCellFormat));
			writableSheet.mergeCells(i * 6 + 7, 0, i * 6 + 12, 0);
		}

		writableSheet.setRowView(1, 500);
		for (i = 0; i < num; i++) {
			writableSheet.addCell(new Label(i * 6 + 7, 1,
					"\u666E\u901A\u52A0\u73ED", titleCellFormat));
			writableSheet.mergeCells(i * 6 + 7, 1, i * 6 + 8, 1);
			writableSheet.addCell(new Label(i * 6 + 9, 1,
					"\u5468\u672B\u52A0\u73ED", titleCellFormat));
			writableSheet.mergeCells(i * 6 + 9, 1, i * 6 + 10, 1);
			writableSheet.addCell(new Label(i * 6 + 11, 1,
					"\u8282\u5047\u65E5\u52A0\u73ED", titleCellFormat));
			writableSheet.mergeCells(i * 6 + 11, 1, i * 6 + 12, 1);
		}

		writableSheet.setRowView(2, 500);
		for (i = 0; i < num; i++) {
			writableSheet.addCell(new Label(i * 6 + 7, 2,
					"\u5DF2\u7528\u989D\u5EA6", titleCellFormat));
			writableSheet.addCell(new Label(i * 6 + 8, 2,
					"\u5269\u4F59\u989D\u5EA6", titleCellFormat));
			writableSheet.addCell(new Label(i * 6 + 9, 2,
					"\u5DF2\u7528\u989D\u5EA6", titleCellFormat));
			writableSheet.addCell(new Label(i * 6 + 10, 2,
					"\u5269\u4F59\u989D\u5EA6", titleCellFormat));
			writableSheet.addCell(new Label(i * 6 + 11, 2,
					"\u5DF2\u7528\u989D\u5EA6", titleCellFormat));
			writableSheet.addCell(new Label(i * 6 + 12, 2,
					"\u5269\u4F59\u989D\u5EA6", titleCellFormat));
		}

		writableSheet.mergeCells(0, 0, 0, 2);
		writableSheet.mergeCells(1, 0, 1, 2);
		writableSheet.mergeCells(2, 0, 2, 2);
		writableSheet.mergeCells(3, 0, 3, 2);
		writableSheet.mergeCells(4, 0, 4, 2);
		writableSheet.mergeCells(5, 0, 5, 2);
		writableSheet.mergeCells(6, 0, 6, 2);
	}
}
